const Base = require('../common/base.js');

module.exports = class extends Base {
	constructor(ctx) {
		super(ctx);
		this.links = this.appModel("links", "dwz");
		this.linkssetup = this.appModel("setup", "dwz");
		this.instr = "huitaor";
	}

	async indexAction() {
		if(this.isGet) {
			return this.appDisplay();
		}

		let posts = this.post();

		let where = {};
		const data = await this.links.dataTable(posts, where);
		console.log(data)
		return this.json(data);

	}
	async setupAction() {
		if(this.isGet) {
			return this.appDisplay();
		}

		let posts = this.post();

		let where = {};
		const data = await this.linkssetup.dataTable(posts, where);
		console.log(data)
		return this.json(data);

	}

	async createAction() {
		let appid = think.uuid('v1').replace(/-/g, '');
		let appkey = think.md5(this.instr + appid + this.instr);

		await this.linkssetup.add({
			appid: appid,
			appkey: appkey,
			create_time: new Date().valueOf(),
			expire_time: new Date().valueOf() + 365 * 24 * 3600 * 1000

		});
		
		return this.success();

	}
	async updateAction() {
		let id = this.post("id");
		let linksapp = await this.linkssetup.findOne(id);
		if(think.isEmpty(linksapp)) {
			return this.fail("应用秘钥不存在");
		}
		await this.linkssetup.setOne(id,{
			expire_time: linksapp.expire_time + 365 * 24 * 3600 * 1000

		});
		
		return this.success();

	}

};